package com.smh.yunpicturebackend.utils;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author shiminghui
 * @date 2025/6/25 17:53
 * @description: 颜色工具类
 */
public class ColorUtils {
    /**
     * 根据16进制字符串获取颜色
     */
    public static Color getColor(String hex) {
        if (hex.startsWith("0x")) {
            hex = hex.substring(2);
        }
        if (hex.startsWith("#")) {
            hex = hex.substring(1);
        }
        int red = Integer.parseInt(hex.substring(0, 2), 16);
        int green = Integer.parseInt(hex.substring(2, 4), 16);
        int blue = Integer.parseInt(hex.substring(4, 6), 16);
        return new Color(red, green, blue);
    }

    /**
     * 根据Color 生成对应的16进制字符串
     */
    public static String getHex(Color color) {
        return String.format("0x%02x%02x%02x", color.red, color.green, color.blue);
    }

    public static void main(String[] args) {
        Color color = getColor("0xFFFFFF");
        System.out.println(color);
        System.out.println(getHex(color));
    }

    @Data
    @AllArgsConstructor
    public static class Color {
        private int red;
        private int green;
        private int blue;
    }
}
